package indi.zhifa.learn.jenkins.helloworld2.web.controller.api;

import indi.zhifa.learn.jenkins.helloworld2.common.entity.RestResponse;
import indi.zhifa.learn.jenkins.helloworld2.web.entity.dto.TokenObject;
import indi.zhifa.learn.jenkins.helloworld2.web.entity.po.AnalysisTask;
import indi.zhifa.learn.jenkins.helloworld2.web.entity.request.DocKeyPassConfig;
import indi.zhifa.learn.jenkins.helloworld2.web.service.IDocAnalysisService;
import indi.zhifa.learn.jenkins.helloworld2.web.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "DocAnalysis:文档分析")
@RequestMapping("/api/v1.0/docAnalysisTask")
@AllArgsConstructor
@RestController
public class DocAnalysisApi {

    private final IDocAnalysisService mDocAnalysisService;
    private final TokenUtil mTokenUtil;

//    @Operation(summary = "上传解析文档")
//    @PostMapping("/uploadAndAnalysis")
//    public RestResponse<DataAnalysisResult> uploadModel(
//            //@ApiParam(value = "解析参数") @RequestBody DocKeySet pDocKeySet,
//            @ApiParam(value = "文件上传") @RequestPart("file") MultipartFile pFile){
//
//        PDDocument document = null;
//        try{
//            document = Loader.loadPDF(pFile.getInputStream());
//            DocKeySet pDocKeySet = new DocKeySet();
//            List<DocKeySetItem> data = new ArrayList<>();
//            pDocKeySet.setData(data);
//
//            DocKeySetItem docKeySetItem1 = new DocKeySetItem();
//            docKeySetItem1.setKey("a");
//            docKeySetItem1.setRegexPrefix("有 1 条南北走向");
//            docKeySetItem1.setRegexSuffix("kV 线路");
//            docKeySetItem1.setTimes(1);
//            docKeySetItem1.setOutlines(new String[]{"1  工程概述","1.2  工程概况 "});
//            data.add(docKeySetItem1);
//
//            DocKeySetItem docKeySetItem2 = new DocKeySetItem();
//            docKeySetItem2.setKey("b");
//            docKeySetItem2.setRegexPrefix("用电量达到");
//            docKeySetItem2.setRegexSuffix("×");
//            docKeySetItem2.setTimes(2);
//            docKeySetItem2.setOutlines(new String[]{"2  电力系统一次","2.1  电力系统概况"});
//            data.add(docKeySetItem2);
//
//            DataAnalysisResult dataAnalysisResult = mDocAnalysisService.analysis(document, pDocKeySet);
//            return RestResponse.ok(dataAnalysisResult);
//        }catch (Exception ex){
//            ex.printStackTrace();
//            return RestResponse.error("加载pdf文档失败，失败信息是 "+ex.toString());
//        }
//    }

    @Operation(summary = "创建任务")
    @PostMapping("")
    public RestResponse<AnalysisTask> create(
            @ApiParam(value = "创建任务") @RequestBody DocKeyPassConfig pDocKeySetConfig){
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        AnalysisTask analysisTask = mDocAnalysisService.create(tokenObject.getId(),pDocKeySetConfig);
        return RestResponse.ok(analysisTask);
    }

    @Operation(summary = "上传解析文档")
    @PutMapping("/{id}/file")
    public RestResponse<AnalysisTask> uploadFile(
            @ApiParam(value = "任务Id") @PathVariable(name = "id") Long pId,
            @ApiParam(value = "文件上传") @RequestPart("file") MultipartFile pFile){
        AnalysisTask analysisTask = mDocAnalysisService.uploadFile(pId,pFile);
        return RestResponse.ok(analysisTask);
    }

    @Operation(summary = "修改键配置")
    @PutMapping("/{id}/keyConfig")
    public RestResponse<AnalysisTask> upload(
            @ApiParam(value = "任务Id") @PathVariable(name = "id") Long pId,
            @ApiParam(value = "键值配置") @RequestBody DocKeyPassConfig pKeyConfig){
        AnalysisTask analysisTask = mDocAnalysisService.editKeyConfig(pId,pKeyConfig);
        return RestResponse.ok(analysisTask);
    }


    @Operation(summary = "解析任务")
    @PostMapping("/{id}/analysis")
    public RestResponse<AnalysisTask> analysis(
            @ApiParam(value = "任务Id") @PathVariable(name = "id") Long pId){
        AnalysisTask analysisTask = mDocAnalysisService.analysis(pId);
        return RestResponse.ok(analysisTask);
    }

    @Operation(summary = "获取解析任务信息")
    @GetMapping("/{id}")
    public RestResponse<AnalysisTask> info(
            @ApiParam(value = "任务Id") @PathVariable(name = "id") Long pId){
        AnalysisTask analysisTask = mDocAnalysisService.info(pId);
        return RestResponse.ok(analysisTask);
    }

    @Operation(summary = "获取任务列表")
    @GetMapping("/list")
    public RestResponse<List<AnalysisTask>> list(){
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        List<AnalysisTask> analysisTaskList = mDocAnalysisService.list(tokenObject.getId());
        return RestResponse.ok(analysisTaskList);
    }

    @Operation(summary = "删除任务")
    @DeleteMapping("/{id}")
    public RestResponse<String> delete(
            @ApiParam(value = "任务Id") @PathVariable(name = "id") Long pId){
        mDocAnalysisService.deleteTask(pId);
        return RestResponse.ok("删除成功");
    }


}
